<template>
  <div class="article-comments">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <!-- <van-cell v-for="(item, index) in commentList" :key="index"> -->
      <van-cell v-for="item in commentList" :key="item.com_id.toString()">
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px;"
          :src="item.aut_photo"
        />
        <span style="color: #466b9d;" slot="title">{{ item.aut_name }}</span>
        <div slot="label">
          <p style="color: #363636;">{{ item.content }}</p>
          <p>
            <span style="margin-right: 10px;">{{
              item.pubdate | fromNow
            }}</span>
            <van-button size="mini" type="default" @click="showReply(item)"
              >回复{{ item.reply_count }}</van-button
            >
          </p>
        </div>
        <van-icon
          slot="right-icon"
          :name="item.is_liking ? 'like' : 'like-o'"
          @click="addLike(item)"
        />
      </van-cell>
    </van-list>
    <!-- 评论列表 -->

    <!-- 发布评论 -->
    <van-cell-group class="publish-wrap">
      <van-field
        clearable
        placeholder="请输入评论内容"
        v-model="newComment"
        @keyup.enter="add"
      >
        <van-button slot="button" size="mini" type="info" @click="add"
          >发布</van-button
        >
      </van-field>
    </van-cell-group>

    <!-- /发布评论 -->
    <van-action-sheet
      v-model="isShowReply"
      title="评论回复"
      style="height: 60%"
    >
      <CommentReply
        v-if="isShowReply"
        :comment="comment"
        @addCount="addCount"
      ></CommentReply>
    </van-action-sheet>
  </div>
</template>

<script>
import {
  getCommentList,
  addComment,
  commentLike,
  deleteCommentLike
} from '@/api/article'
import CommentReply from '@/views/article/commentReply.vue'
export default {
  name: 'ArticleComment',
  components: {
    CommentReply
  },
  computed: {
    articleId() {
      return this.$route.params.id
    }
  },
  data() {
    return {
      commentList: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      newComment: '',
      offset: null,
      isShowReply: false,
      comment: ''
    }
  },

  methods: {
    async onLoad() {
      // 异步更新数据
      const res = await getCommentList(this.articleId, this.offset)
      this.commentList = [...this.commentList, ...res.data.results]
      this.loading = false
      console.log(res)
      if (res.data.results.length === 0 || this.commentList.length >= 40) {
        this.finished = true
      }
      this.offset = res.data.last_id
    },
    async add() {
      const res = await addComment(this.articleId, this.newComment)

      this.commentList.unshift(res.data.new_obj)
      console.log(res)
      this.newComment = ''
    },
    async addLike(item) {
      if (item.is_liking) {
        await deleteCommentLike(item.com_id)
      } else {
        const res = await commentLike(item.com_id)
        console.log(res)
      }
      item.is_liking = !item.is_liking
      this.$toast.success('操作成功')
    },
    showReply(item) {
      this.comment = item
      this.isShowReply = true
    },
    addCount() {
      this.comment.reply_count++
    }
  }
}
</script>

<style scoped lang="less">
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
</style>
